package com.zhanghp.demo02;

import com.zhanghp.demo02.state.IReadyState;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhanghp
 * @date 2022-09-03 9:52
 */
@Slf4j
public class IPlayer {
    /**
     * 播放机状态
     */
    private IState iState;
    /**
     * 歌曲列表
     */
    private final static List<String> songs = new ArrayList<>();
    /**
     * 当前歌曲索引
     */
    private static int currentSongIndex;
    /**
     * 是否播放
     */
    private boolean playing = false;

    static {
        currentSongIndex = 0;
        // 音乐歌曲
        for (int i = 1; i <= 12; i++) {
            songs.add("Song:" + i);
        }
    }

    public IPlayer() {
        iState = new IReadyState(this);
    }

    public void changeState(IState iState){
        this.iState = iState;
    }

    public void setPlaying(boolean playing){
        this.playing = playing;
    }

    public IState getState(){
        return this.iState;
    }

    public void play(){
        log.info("Playing -> " + songs.get(currentSongIndex));
    }

    public void next(){
        if (++ currentSongIndex >= songs.size()) {
            currentSongIndex = 0;
        }
        log.info("Playing -> {}", songs.get(currentSongIndex));
    }

    public void previous(){
        if (-- currentSongIndex < 0) {
            currentSongIndex = songs.size() - 1;
        }
        log.info("Playing -> {}", songs.get(currentSongIndex));
    }

}
